package lol.clann.minecraft.springboot.plugin.core.database.datasource;

import lol.clann.minecraft.springboot.api.annotation.config.Configure;
import lol.clann.minecraft.springboot.api.annotation.reload.PostReload;
import lombok.Getter;
import org.springframework.stereotype.Service;

/**
 *
 * @author pyz
 * @date 2019/6/1 10:20 AM
 */
@Service
@Getter
public class DataSourceConfig {

    @Configure(value = "springbootplugin.datasource.enable", comment = "是否启用数据库")
    private boolean enable = true;

    @Configure(value = "springbootplugin.datasource.useMysql", comment = "是否使用mysql(默认是用h2)")
    private boolean useMysql = false;

    @Configure(value = "springbootplugin.datasource.logsql", comment = "是否记录sql日志")
    private boolean logsql = false;

    @Configure(value = "springbootplugin.datasource.url", comment = "数据库地址,只对mysql生效")
    private String dbUrl = "jdbc:mysql://host:port/database?useUnicode=true&amp;characterEncoding=utf8mb4";

    @Configure(value = "springbootplugin.datasource.h2Url", comment = "数据库地址,只对h2生效,可自定义")
    private String h2Url = "jdbc:h2:file:${dir}/data/SpringBoot/db;IGNORE_UNKNOWN_SETTINGS=TRUE;MODE=MYSQL;AUTO_SERVER=TRUE;CACHE_SIZE=10240";

    @Configure(value = "springbootplugin.datasource.h2BindAddress", comment = "某些面板获取不到localhost,直接指定bindAddress")
    private String h2BindAddress = "127.0.0.1";

    @Configure(value = "springbootplugin.datasource.username", comment = "数据库账号,只对mysql生效")
    private String username = "";

    @Configure(value = "springbootplugin.datasource.password", comment = "数据库密码,只对mysql生效")
    private String password = "";

    @Configure("springbootplugin.datasource.initialSize")
    private int initialSize = 10;

    @Configure("springbootplugin.datasource.minIdle")
    private int minIdle = 10;

    @Configure(value = "springbootplugin.datasource.maxActive", comment = "最大连接数")
    private int maxActive = 100;

    @Configure("springbootplugin.datasource.maxWait")
    private int maxWait = 60000;

    @Configure("springbootplugin.datasource.timeBetweenEvictionRunsMillis")
    private int timeBetweenEvictionRunsMillis = 60000;

    @Configure("springbootplugin.datasource.minEvictableIdleTimeMillis")
    private int minEvictableIdleTimeMillis = 300000;

    @Configure("springbootplugin.datasource.validationQuery")
    private String validationQuery = "SELECT 1 FROM DUAL";

    @Configure("springbootplugin.datasource.testWhileIdle")
    private boolean testWhileIdle = true;

    @Configure("springbootplugin.datasource.testOnBorrow")
    private boolean testOnBorrow = false;

    @Configure("springbootplugin.datasource.testOnReturn")
    private boolean testOnReturn = false;

    @Configure("springbootplugin.datasource.poolPreparedStatements")
    private boolean poolPreparedStatements = true;

    @PostReload
    private void reload() {
        DataSourceProcesser.refresh();
    }
}

